package cn.tedu.blog.server.service.impl;

import cn.tedu.blog.server.exception.CoolSharkServiceException;
import cn.tedu.blog.server.mapper.BannerMapper;
import cn.tedu.blog.server.pojo.dto.BannerAddDTO;
import cn.tedu.blog.server.pojo.dto.BannerEditDTO;
import cn.tedu.blog.server.pojo.entity.Banner;
import cn.tedu.blog.server.pojo.vo.BannerDetailVO;
import cn.tedu.blog.server.pojo.vo.BannerListVO;
import cn.tedu.blog.server.service.IBannerService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.ResponseCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BannerServiceImpl implements IBannerService {
    @Autowired
    private BannerMapper bannerMapper;
    
    // 添加轮播图
    @Override
    public void insertBanner(BannerAddDTO bannerAddDTO) {
        // 先根据路径查询是否数据库中已存在
        BannerDetailVO vo = bannerMapper.getByUrl(bannerAddDTO.getUrl());
        if (vo != null) {
            throw new CoolSharkServiceException(ResponseCode.CONFLICT, "添加失败,轮播图已存在");
        }
        // 代码走到此处说明轮播图数据库没有
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerAddDTO, banner);
        // 补全属性
        banner.setGmtCreate(LocalDateTime.now());
        int rows = bannerMapper.insertBanner(banner);
        if (rows != 1) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR," 添加失败, 服务器忙, 请稍后再试");
        }
    }   
    
    // 删除轮播图
    @Override
    public void deleteBanner(Long id) {
        // 先根据id查询, 无数据抛异常
        BannerDetailVO bannerDetailVO = bannerMapper.getById(id);
        if (bannerDetailVO == null) {
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, "要删除的轮播图不存在");
        }
        int rows = bannerMapper.deleteBanner(id);
        if (rows != 1) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "删除失败, 服务器忙");
        }

    }
    
    // 批量删除轮播图
    @Override
    public void deleteBannerByIds(Long... ids) {
        int rows = bannerMapper.deleteBannerByIds(ids);
        log.info("成功删除{}条轮播图", rows);
    }

    // 修改轮播提
    @Override
    public void updateBanner(BannerEditDTO bannerEditDTO) {
        // 根据id查询轮播图, 不存在抛出异常
        BannerDetailVO bannerDetailVO = bannerMapper.getById(bannerEditDTO.getId());
        if (bannerDetailVO == null) {
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND, "要修改的轮播图不存在");
        }
        // 执行修改
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerEditDTO, banner);
        // 补全属性
        banner.setGmtModified(LocalDateTime.now());
        int rows = bannerMapper.updateBanner(banner);
        if (rows != 1) {
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR, "服务器忙, 请稍后再试");
        }
    }
    // 分页查询轮播图
    @Override
    public JsonPage<BannerListVO> listBanner(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<BannerListVO> bannerListVOS = bannerMapper.listBanner();
        return JsonPage.restPage(new PageInfo<>(bannerListVOS));
    }
    
    // 分页查询轮播图status=1的
    @Override
    public JsonPage<BannerListVO> listBannerByStatus1(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<BannerListVO> bannerListVOS = bannerMapper.listBannerByStatus1();
        return JsonPage.restPage(new PageInfo<>(bannerListVOS));
    }
    // 分页查询轮播图status=0的
    @Override
    public JsonPage<BannerListVO> listBannerByStatus0(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<BannerListVO> bannerListVOS = bannerMapper.listBannerByStatus0();
        return JsonPage.restPage(new PageInfo<>(bannerListVOS));
    }

    /*------前台部分---------*/
    // 查询所有status=1的轮播图
    @Override
    public List<BannerListVO> listApiBanner() {
        List<BannerListVO> bannerListVOS = bannerMapper.listBannerByStatus1();
        return bannerListVOS;
    }


}
